const phone = document.getElementById("phone");
const adminNo = document.getElementById("adminNo");
const password = document.getElementById("password");
const rePassword = document.getElementById("rePassword");
const submit = document.getElementById("submit")

var un=false,pw=false,pw2=false,tel=false;

phone.onblur = function(){
    var label = this.nextElementSibling;
    if(/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(this.value)){
        label.innerHTML = "ok";
        tel = true;
    }else{
        label.innerHTML = "no";
        tel = false;
    }
}

adminNo.onblur = function(){
    var label = this.nextElementSibling;
    if(/^[\u2E80-\u9FFF\w\-]{4,20}$/.test(this.value)){
        label.innerHTML = "ok";
        un = true;
    }else{
        label.innerHTML = "no";
        un = false;
    }
}


password.onblur = function(){
    var label = this.nextElementSibling;
    // 是否包含数字
    var a = /\d/.test(this.value) ? 1 : 0;
    // 是否包含字母
    var b = /[a-z]/i.test(this.value) ? 1 : 0;
    // 是否包含特殊
    var c = /[^a-z\d]/i.test(this.value) ? 1 : 0;
    switch(a+b+c){
        case 1:label.innerHTML = "弱";break;
        case 2:label.innerHTML = "中";break;
        case 3:label.innerHTML = "强";break;
    }
    pw = true;

    if(rePassword.value === "") return;
    if(this.value === rePassword.value){
        rePassword.nextElementSibling.innerHTML = "一致"
        pw2 = true;
    }else{
        rePassword.nextElementSibling.innerHTML = "不一致"
        pw2 = false;
    }
}

rePassword.onblur = function(){
    var label = this.nextElementSibling;
    if(this.value === password.value){
        label.innerHTML = "一致"
        pw2 = true;
    }else{
        label.innerHTML = "不一致"
        pw2 = false;
    }
}


submit.onclick = function(){
    if(un && pw && pw2 && tel){
        ajax("http://localhost:3000/api",function(res){
            console.log(res);
            res = JSON.parse(res);
    
            if(res.code === 0){
                alert(res.title);
                adminNo.value = "";
                adminNo.focus();
            }else if(res.code === 1){
                if(confirm("注册成功，是否跳转到登录")){
                    location.href = "./login.html";
                }
            }
    
        },{
            type:"register",
            username:adminNo.value,
            password:rePassword.value
        })

    }else{
        alert("失败")
    }
}

// submit.onclick = function(){
//     ajax("http://localhost:3000/api",function(res){
//         console.log(res);
//         res = JSON.parse(res);

//         if(res.code === 0){
//             alert(res.title);
//             adminNo.value = "";
//             adminNo.focus();
//         }else if(res.code === 1){
//             if(confirm("注册成功，是否跳转到登录")){
//                 location.href = "./login.html";
//             }
//         }

//     },{
//         type:"register",
//         username:adminNo.value,
//         password:rePassword.value
//     })
// }


function ajax(url, callback, data){
    let str = "";
    for(let i in data){
        str += `${i}=${data[i]}&`
    }
    url += "?" + str;
    const xhr = new XMLHttpRequest();
    xhr.open("get",url)
    xhr.onload = function(){
        if(xhr.status === 200){
            callback(xhr.responseText)
        }
    }
    xhr.send();
}